const {ccclass, property} = cc._decorator;

/**换三张的提示的脚本 */
@ccclass
export default class HanSanZhang extends cc.Component {

    /**换三张的提示的总容器 */
    huanPaiTip: cc.Node = null;

    huanPaiArr = [];

    onLoad() {
        this.huanPaiTip = cc.find('Canvas/huansanzhang');
        this.huanPaiTip.active = cc.vv.gameNetMgr.isHuanSanZhang;

        if(this.huanPaiTip.active){
            this.showHuanPai(cc.vv.gameNetMgr.getSelfData().huanPais == null);
        }

        // 换三张的确定按钮
        const btnOk = cc.find('Canvas/huansanzhang/btn_ok');
        if(btnOk){
            cc.vv.utils.addClickEvent(btnOk, this.node, 'HuanSanZhang', 'onHuanSanZhang');
        }

        this.node.on(cc.vv.game_event_map.GAME_BEGIN, () => {
            this.initHuanPaiInfo();
        });

        this.node.on(cc.vv.game_event_map.GAME_HUANPAI, () => {
            this.huanPaiTip.active = true;
            this.showHuanPai(true);
        });

        this.node.on(cc.vv.game_event_map.HUANPAI_NOTIFY, (data) => {
            if(data.detail.seatIndex == cc.vv.gameNetMgr.seatIndex){
                this.initHuanPaiInfo();
            }
        });

        this.node.on(cc.vv.game_event_map.GAME_HUANPAI_OVER, (data) => {
            this.huanPaiTip.active = false;
            for(let i = 0; i < this.huanPaiArr.length; i++){
                this.huanPaiArr[i].y = 0;
            }
            this.huanPaiArr = [];
            this.initHuanPaiInfo();
        });


        this.node.on(cc.vv.game_event_map.MJ_CLICKED, (data) => {
            const target = data.detail;
            // 如果已经点起来，则取消
            const idx = this.huanPaiArr.indexOf(target);
            if(idx != -1){
                target.y = 0;
                this.huanPaiArr.splice(idx, 1);
            }
            else{
                // 如果是新的，并且换牌数不够3张，则加入
                if(this.huanPaiArr.length < 3){
                    this.huanPaiArr.push(target);
                    target.y = 15;
                }
            }
        });
    }

    showHuanPai(interactable){
        this.huanPaiTip.getChildByName('info').getComponent(cc.Label).string = interactable ? '请选择三张一样花色的牌' : '等待其他玩家选牌...';
        this.huanPaiTip.getChildByName('btn_ok').getComponent(cc.Button).interactable = interactable;
        this.huanPaiTip.getChildByName('mask').active = false;
    }

    initHuanPaiInfo(){
        const huanPaiInfo = cc.find('Canvas/game/huanpaiinfo');
        const seat = cc.vv.gameNetMgr.getSelfData();
        if(seat.huanPais == null){
            huanPaiInfo.active = false;
            return;
        }

        huanPaiInfo.active = true;
        for(let i = 0 ; i < seat.huanPais.length; i++){
            huanPaiInfo.getChildByName('hp' + (i + 1)).getComponent(cc.Sprite).spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(cc.vv.preAtlasString.M, seat.huanPais[i]);
        }

        const hpm = huanPaiInfo.getChildByName('hpm');
        hpm.active = true;
        switch(cc.vv.gameNetMgr.huanPaiMethod){
            case 0:
                hpm.rotation = 90;
                break;
            case 1:
                hpm.rotation = 0;
                break;
            case 2:
                hpm.rotation = 180;
                break;
            default:
                hpm.active = false;
        }
    }


    /**换三张的确定按钮的点击事件 */
    onHuanSanZhang(event: cc.Event){
        if(this.huanPaiArr.length != 3){
            return;
        }

        let type = null;
        for(let i = 0; i < this.huanPaiArr.length; i++){
            const pai = this.huanPaiArr[i].mjId;
            const nt = cc.vv.mahjongmgr.getMahjongType(pai);
            if(type == null){
                type = nt;
            }
            else{
                if(type != nt){
                    return;
                }
            }
        }

        const data = {
            p1: this.huanPaiArr[0].mjId,
            p2: this.huanPaiArr[1].mjId,
            p3: this.huanPaiArr[2].mjId,
        };

        this.huanPaiTip.getChildByName('info').getComponent(cc.Label).string = '等待其他玩家选牌...';
        this.huanPaiTip.getChildByName('btn_ok').getComponent(cc.Button).interactable = false;
        this.huanPaiTip.getChildByName('mask').active = true;

        cc.vv.net.send(cc.vv.game_event_map.HUANPAI, data);
    }
}
